//
//  YYYuYinNetWorkManger.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/12.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

import UIKit

class YYYuYinNetWorkManger: NSObject {
    private var  mgr:AFHTTPSessionManager!
    
    static let defultManger = YYYuYinNetWorkManger.init();
    public override init() {
        super.init();
        self.mgr = AFHTTPSessionManager.init(baseURL: nil);
        //固定头部
        mgr.requestSerializer.setValue("mg/9 (iPhone; iOS 10.2; Scale/3.00)", forHTTPHeaderField: "User-Agent")
        mgr.requestSerializer.setValue("zh-Hans-CN;q=1", forHTTPHeaderField: "Accept-Language")
        mgr.requestSerializer.setValue("gzip, deflate", forHTTPHeaderField: "Accept-Encoding")
        mgr.requestSerializer.setValue("uid=(null)", forHTTPHeaderField: "Cookie");

        
        
        
        
    }

    
    //post
    public func postNetwork(urlString:String,dic:Dictionary<String, Any>,successHandle:@escaping ( _ json:Any) -> Void,failHandle:@escaping (_ des:String) -> Void) ->Void{
        
        YYYuYinNetWorkManger.defultManger.mgr.post(urlString, parameters: dic, progress: { (progess) in
            
        }, success: { (task, json) in
            
            successHandle(json as Any);
            
            
        }) { (task, error) in
            
            failHandle(error.localizedDescription);
        }
        
        
        
        
    }
    //get
    public func getNetwork(urlString:String,dic:Dictionary<String, Any>,isJson:Bool,successHandle:@escaping ( _ json:Any) -> Void,failHandle:@escaping (_ des:String) -> Void) ->Void{
        
        if isJson {

            self.mgr.responseSerializer = AFJSONResponseSerializer();
        }else{
        
            self.mgr.responseSerializer = AFHTTPResponseSerializer();
            
        }
        YYYuYinNetWorkManger.defultManger.mgr.get(urlString, parameters: dic, progress: { (progess) in
            
        }, success: { (task, json) in
            
            successHandle(json! );
            
            
        }) { (task, error) in
            
            failHandle(error.localizedDescription);
        }
        
        
        
        
}

}
